<script>
import { appointmentCancelOrBack, appointmentToPay, selAppointmentRecord } from '@/services/task'

export default {
  data () {
    return {
      tableData: [],
      dialogVisible: false,
      qrCodeSrc: ''
    }
  },
  created () {
    this.loadRecords()
  },
  methods: {
    async loadRecords () {
      const { data } = await selAppointmentRecord(this.$route.params.id)
      if (data.code === 200) {
        this.tableData = data.data
      }
    },
    cancel () {
      this.$router.push('/task')
    },
    async handleEdit (rowData) {
      console.log(rowData)
      const { data } = await appointmentCancelOrBack({
        id: this.$route.params.id,
        sysAppointmentId: rowData.sysAppointmentId
      })
      if (data.code === 200) {
        this.$message.success('取消预约成功')
        this.loadRecords()
      }
    },
    async handleToPay (rowData) {
      const { data } = await appointmentToPay({
        id: this.$route.params.id,
        sysAppointmentId: rowData.sysAppointmentId
      })
      if (data.code === 200) {
        this.dialogVisible = true
        this.qrCodeSrc = `data:image/png;base64,${data.data}`
        this.$message.success('操作成功')
      }
    }
  },
  filters: {
  // 日期过滤器
    dateFormat (date) {
      if (date === null) {
        return null
      }
      date = new Date(date)
      return `
        ${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}
      `
    },
    rangeFormat (value) {
      if (value === 0) {
        return '上午'
      } else {
        return '下午'
      }
    }
  }
}
</script>

<template>
  <div>
    <el-page-header @back="cancel" content="记录页面">
    </el-page-header>
    <el-table
      :data="tableData"
      stripe
      style="width: 100%">
      <el-table-column
        prop="hospitalName"
        label="院区"
        width="180">
      </el-table-column>
      <el-table-column
        prop="deptName"
        label="科室"
        width="180">
      </el-table-column>
      <el-table-column
        prop="docName"
        label="医生"
        width="80">
      </el-table-column>
      <el-table-column
        prop="admDate"
        label="就诊日期"
        width="140">
        <template slot-scope="scope">{{ scope.row.admDate | dateFormat}} {{scope.row.admRange | rangeFormat}}</template>
      </el-table-column>
      <el-table-column
        prop="appointmentNo"
        label="诊号"
        width="80">
      </el-table-column>
      <el-table-column
        prop="cardId"
        label="就诊卡ID"
        width="180">
      </el-table-column>
      <el-table-column
        prop="patientName"
        label="就诊人"
        width="80">
      </el-table-column>
      <el-table-column
        prop="admId"
        label="就诊ID"
        width="180">
      </el-table-column>
      <el-table-column
        label="操作"
        width="360">
        <template slot-scope="scope">
          <el-button
            size="mini" v-if="scope.row.appointStatus===2"
            @click="handleEdit(scope.row)">取消预约</el-button>
          <el-button
            size="mini" v-if="scope.row.appointStatus===2"
            @click="handleToPay(scope.row)">去支付</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog
      title="扫码支付"
      :visible.sync="dialogVisible"
      width="240px"
      height="300px"
      >
      <img :src="qrCodeSrc" alt="QR Code" />
      <span slot="footer" class="dialog-footer">
  </span>
    </el-dialog>
  </div>

</template>

<style scoped lang="scss">

</style>
